/*
 * Copyright (C) 2009 Show SMS open source project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bydavy.android.showsms;

//~--- non-JDK imports --------------------------------------------------------

import java.util.ArrayList;

import android.app.Activity;

import android.graphics.Bitmap;

import android.os.Bundle;

import android.telephony.gsm.SmsManager;

import android.text.Editable;

import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bydavy.model.showsms.Person;
import com.bydavy.model.showsms.SSMessage;
import com.bydavy.util.showsms.Log;

/**
 * Quick reply
 *
 * @version        1.0, 09/11/08
 * @author         Davy L.
 */
public class QuickReplyActivity extends Activity {
    //~--- fields -------------------------------------------------------------

    /** Field description */
    private final String BUNDLE_REPLY_TEXT_KEY = "REPLY_TEXT";

    /** Height and Weight of the photo */
    private final int PHOTO_HEIGHT_WIDTH = 80;

    /** Field description */
    private SSMessage message = null;

    /** Field description */
    private TextView bodyTV;

    /** Field description */
    private TextView hourTV;

    /** Field description */
    private TextView nameTV;

    /** Field description */
    private ImageView photoIV;

    /** Field description */
    private EditText quickReplyET;

    /** Field description */
    private Button sendB;

    //~--- methods ------------------------------------------------------------

    /**
     * Method description
     *
     *
     * @param savedInstanceState
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v("QuickReplyActivity : onCreate()");
        setContentView(R.layout.quick_reply);

        // Layout View
        nameTV       = (TextView) findViewById(R.id.PreviewNameTV);
        photoIV      = (ImageView) findViewById(R.id.PreviewPhotoSenderImageView);
        hourTV       = (TextView) findViewById(R.id.PreviewTVHour);
        bodyTV       = (TextView) findViewById(R.id.QuickReplyBodyTV);
        quickReplyET = (EditText) findViewById(R.id.QuickReplyET);

        // bouton envoyer
        sendB = (Button) findViewById(R.id.QuickReplySendB);
        sendB.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessage();
            }
        });
        message = SSMessage.constructFromBundle(getIntent().getExtras());

        String reply = null;

        if (savedInstanceState != null) {
            reply = (String) savedInstanceState.get(BUNDLE_REPLY_TEXT_KEY);
        }

        populateViews(message, reply);
    }

    /**
     * Method description
     *
     */
    public void sendMessage() {
        String reply = quickReplyET.getText().toString();
        String tel      = message.getPerson().getPhoneNumber();
        
        Log.v("QuickReplyActivity : tel " + tel + ", msg " + reply);

        try {
            SmsManager sm = SmsManager.getDefault();
            ArrayList<String> messages = sm.divideMessage(reply);
            sm.sendMultipartTextMessage(tel, null, messages, null, null);
            message.saveReplyDB(getApplicationContext(), reply, System.currentTimeMillis());
            Toast.makeText(this, getString(R.string.quick_reply_msg_sent), Toast.LENGTH_LONG).show();
        } catch (Exception e) {}

        
        finish();
    }

    /**
     * Method description
     *
     *
     * @param theMessage
     * @param reply
     */
    private void populateViews(SSMessage theMessage, String reply) {
        Log.v("PreviewActivity : populateViews()");

        Person person        = theMessage.getPerson();
        Bitmap originalPhoto = person.getPhoto(getApplicationContext());
        Bitmap photo;

        photo = Bitmap.createScaledBitmap(originalPhoto, PHOTO_HEIGHT_WIDTH, PHOTO_HEIGHT_WIDTH, false);
        photoIV.setImageBitmap(photo);
        nameTV.setText(person.getNameOrPhoneNumber());
        hourTV.setText(theMessage.getHour(getApplicationContext()));
        bodyTV.setText(theMessage.getBody());

        // If a reply previously saved exist, we set the EditText
        if (reply != null) {
            quickReplyET.setText(reply);
        }
    }

    /**
     * Method description
     *
     */
    public void onStart() {
        super.onStart();
        Log.v("QuickReplyActivity : onStart()");
    }

    /**
     * When application loose the screen
     *
     */
    public void onStop() {
        super.onStop();
        Log.v("QuickReplyActivity : onStop()");
    }

    /**
     * Method description
     *
     *
     * @param outState
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.v("QuickReplyActivity: onSaveInstanceState()");

        // We save the message currently write
        Editable reply = quickReplyET.getText();

        outState.putString(BUNDLE_REPLY_TEXT_KEY, reply.toString());
    }

    /**
     * Method description
     *
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.v("QuickReplyActivity: onDestroy()");
    }
}
